package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 套餐分页查询业务逻辑
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        //和接口文档一致
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐业务逻辑
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        //1.添加套餐基本信息
            //属性拷贝
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
            //直接插入
        setmealMapper.insert(setmeal);

        //2.批量添加关联的菜品信息
            //2.1获取套餐主键id
        Long setmealId = setmeal.getId();
            //2.2获取菜品数据集合
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
            //2.3 遍历集合， 对每个菜品设置套餐的主键id 对象.setter()
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        //批量插入菜品
        setmealDishMapper.insertBatch(setmealDishes);

    }

    /**
     * 根据id查询套餐业务逻辑
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmealVO = setmealMapper.getByIdWithDish(id);
        return setmealVO;
    }

    /**
     * 修改套餐业务逻辑
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //属性拷贝
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //1.修改套餐基本信息
        setmealMapper.update(setmeal);

        //2.修改菜品信息  先删除菜品setmealDish(setmealId) 再添加(添加逻辑)
        Long setmealId = setmealDTO.getId();
        //根据关联的套餐id删除菜品信息
        setmealDishMapper.deleteBySetmealId(setmealId);

        //添加
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));

        //批量插入
        setmealDishMapper.insertBatch(setmealDishes);

    }

    /**
     * 批量删除套餐业务逻辑
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        //起售中的套餐不能删除
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if (StatusConstant.ENABLE.equals(setmeal.getStatus())) {
                //抛出业务异常
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //删除套餐表中的数据--遍历
        ids.forEach(setmealId -> {
            setmealMapper.deleteById(setmealId);
            //删除套餐关联的菜品数据
            setmealDishMapper.deleteBySetmealId(setmealId);
        });

    }

    /**
     * 套餐起售、停售业务逻辑
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售菜品--有：抛异常  没有：执行下一步 semealMapper.update(status, id)
        if (status.equals(StatusConstant.ENABLE)) {
            List<Dish> dishList = dishMapper.getBySetmealId(id);  //需要联表查询,另写一个方法
            //判断是否有菜品
            if (dishList != null && dishList.size() > 0) {
                //遍历集合
                dishList.forEach(dish -> {
                    //此时有菜品，判断状态是否为停售，无法开启售卖
                    if (StatusConstant.DISABLE.equals(dish.getStatus())) {
                        //抛异常， 套餐内包含未启售菜品，无法启售
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        //起售设置状态
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);


    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
